package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.cache.service.IDestinationStatisService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisService;
import cn.wolfcode.luowowo.cache.service.ITravelStatisService;
import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.domain.TravelComment;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import cn.wolfcode.luowowo.comment.service.ITravelCommentService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import jdk.management.resource.internal.inst.FileOutputStreamRMHooks;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/mine")
public class MineController {
    @Reference
    private ITravelService travelService;
    @Reference
    private IUserInfoService userInfoService;
    @Reference
    private ITravelCommentService travelCommentService;
    @Reference
    private IStrategyCommentService strategyCommentService;
    @Reference
    private IStrategyStatisService strategyStatisService;
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private ITravelStatisService travelStatisService;
    @Reference
    private IDestinationStatisService destinationStatisService;
    @Reference
    private IDestinationService destinationService;

    @RequestMapping("/home")
    public String home(Model model,@UserParam UserInfo userInfo){
        //查询作者游记
        List<Travel> travelList =travelService.selectByAuthorId(userInfo.getId());
        model.addAttribute("travels",travelList);

        //查询作者关注的user
        List<UserInfo> idols = userInfoService.getIdol(userInfo.getidolIds());
        model.addAttribute("idols",idols);
        System.out.println(travelList.subList(0,1));

        //查询作者评论的游记
        List<TravelComment> travelComments =travelCommentService.selectByUserId(userInfo.getId());
        model.addAttribute("travelComments",travelComments);
        return "/mine/homepage";
    }

    @RequestMapping("/setting")
    public String setting(Model model,@UserParam UserInfo userInfo){
        Long id = userInfo.getId();
        userInfo = userInfoService.getUserInfoById(id);
        model.addAttribute("userInfo",userInfo);
        return"/mine/setting";
    }

    @RequestMapping("/update")
    @ResponseBody
    public Object update(UserInfo userInfo){
        userInfoService.updateUserInfo(userInfo);

        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setMsg("修改成功");
        return ajaxResult;
    }


    @RequestMapping("/travel")
    public String travel(Model model, @ModelAttribute("qo") TravelQuery qo, @UserParam UserInfo userInfo){
        //查询作者游记

        qo.setAuthorId(userInfo.getId());
        PageInfo pageInfo = travelService.query(qo);
        List<Travel> travelList = pageInfo.getList();
        model.addAttribute("pageInfo",pageInfo);
        int replynum = 0;
        int viewnum = 0;
        for (Travel travel : travelList) {
            replynum += travel.getReplynum();
            viewnum += travel.getViewnum();
        }
        model.addAttribute("replynum",replynum);
        model.addAttribute("viewnum",viewnum);
        return "/mine/mytravelnotes";
    }
    //sendVerificode
    @ResponseBody
    @RequestMapping("/sendVerificode")
    public Object sendVerificode(String phone){
        System.out.println(phone);
        //发送验证码
        userInfoService.sendVerifyCode(phone,0);
        return new AjaxResult();
    }

    @RequestMapping("/updatePassword")
    public String updatePassword(){
        return "/mine/updatePassword";
    }

    @ResponseBody
    @RequestMapping("/setNewPassword")
    public Object setNewPassword(@UserParam UserInfo userInfo,String password,String rpassword,String verifyCode){
        userInfoService.setNewPassword(userInfo,password,rpassword,verifyCode);
        return new AjaxResult(true,"修改成功");
    }

    @ResponseBody
    @RequestMapping("/updatePhoneSendVerificode")
    public Object updatePhoneSendVerificode(@UserParam UserInfo userInfo, String phone){
        AjaxResult result = new AjaxResult();
        //判断输入手机号码与绑定手机号码
        if (!phone.equals(userInfo.getPhone())){
            result.setSuccess(false);
            result.setMsg("输入手机号码与绑定手机号码不一致,请重新输入");
            return result;
        }else {
            //发送验证码
            userInfoService.sendVerifyCode(phone, 0);
            return result;
        }
    }
    @ResponseBody
    @RequestMapping("/updateNewPhoneSendVerificode")
    public Object updateNewPhoneSendVerificode(@UserParam UserInfo userInfo, String phone){
        AjaxResult result = new AjaxResult();
        //判断输入手机号码与绑定手机号码
        if (phone.equals(userInfo.getPhone())){
            result.setSuccess(false);
            result.setMsg("新手机号码不能跟原号码一致");
            return result;
        }else {
            //发送验证码
            userInfoService.sendVerifyCode(phone, 0);
            return result;
        }
    }
    @ResponseBody
    @RequestMapping("/phoneVerify")
    public Object phoneVerify(String phone,String code){
        AjaxResult result = new AjaxResult();
        result.setSuccess(userInfoService.phoneVerify(phone,code));
        return result;
    }

    @ResponseBody
    @RequestMapping("/updatePhone")
    public Object updatePhone(@UserParam UserInfo userInfo,String phone,String code){
        AjaxResult result = new AjaxResult();
        result.setSuccess(userInfoService.updatePhone(userInfo.getId(),phone,code));
        return result;
    }
    @RequestMapping("/logoff")
    @ResponseBody
    public Object logoff(@UserParam UserInfo userInfo,HttpServletRequest request){
        //清除session
        HttpSession session = request.getSession();
        session.invalidate();
        //获取token中的秘钥
        Cookie[] cookies = request.getCookies();
        String token = null;
        for (Cookie cookie : cookies) {
            if ("token".equals(cookie.getName())){
                token = cookie.getValue();
            }
        }
        userInfoService.logoff(userInfo,token);
        return AjaxResult.SUCCESS;
    }

    @RequestMapping("/review")
    public String review(Model model,@UserParam UserInfo userInfo){

        //用户攻略评论
        List<StrategyComment> strategyComments = strategyCommentService.queryByUserId(userInfo.getId());

        //用户游记评论
        List<TravelComment> travelComments = travelCommentService.selectByUserId(userInfo.getId());
        model.addAttribute("strategyComments",strategyComments);
        model.addAttribute("travelComments",travelComments);
        //点赞数
        Long thumbnum = 0L;
        for (StrategyComment strategyComment : strategyComments) {
             thumbnum += strategyComment.getThumbupnum();
        }
        for (TravelComment travelComment : travelComments) {
            thumbnum  += travelComment.getThumbnum();
        }
        model.addAttribute("thumbnum",thumbnum);
        return "/mine/review";
    }

    //用户收藏
    @RequestMapping("/favor")
    public String favor(Model model,@UserParam UserInfo userInfo){

        List<String> strategyDetailIds= strategyStatisService.getUserFavorStrategy(userInfo.getId());
        List<StrategyDetail> strategyDetails = new ArrayList<>();
       for (String strategyDetailId : strategyDetailIds) {
            StrategyDetail strategyDetail = strategyDetailService.get(Long.parseLong(strategyDetailId));
            strategyDetails.add(strategyDetail);
       }
       List<String> travelIds = travelStatisService.getUserFavorTravel(userInfo.getId());
       List<Travel> travelList = new ArrayList<>();
        for (String travelId : travelIds) {
            Travel travel = travelService.get(Long.parseLong(travelId));
            travelList.add(travel);
        }
        List<String> destIds = destinationStatisService.getUserFavorDest(userInfo.getId());
        List<Destination> destinationList = new ArrayList<>();

        for (String destId : destIds) {
            Destination destination = destinationService.getDestById(Long.parseLong(destId));
            Destination country = destinationService.getCountry(Long.parseLong(destId));
            destination.setParent(country);
            destinationList.add(destination);
        }
        model.addAttribute("strategyDetails",strategyDetails);
        model.addAttribute("travels",travelList);
        model.addAttribute("destinations",destinationList);
        return "/mine/travelcollection";
    }


}
